package cfca.paperless.client.servlet.performance;

import java.io.File;
import java.util.Date;

import org.apache.commons.io.FileUtils;

import cfca.paperless.client.bean.SealUserInfo;
import cfca.paperless.client.bean.SignLocation;
import cfca.paperless.client.servlet.PaperlessClient;
import cfca.paperless.client.servlet.PaperlessConfig;
import cfca.paperless.client.util.GUID;
import cfca.paperless.client.util.IoUtil;
import cfca.paperless.client.util.PaperlessUtil;
import cfca.paperless.client.util.StrategyUtil;

public class testSealAutoPdfPerformance {

    public static void main(String[] args) throws Exception {
        try {
            long timeStart = System.currentTimeMillis();

            byte[] pdf = null;//FileUtils.readFileToByteArray(new File("./TestData/file.pdf"));
            //byte[] pdf = FileUtils.readFileToByteArray(new File("./TestData/compoundSealAutoPdfListDetached-2.pdf"));

            SealUserInfo sealUserInfo = new SealUserInfo("张三", "beijing", "reason");
//            SignLocation signLocation = new SignLocation("银行签章", "C", 0, 0);signLocation.setPage(1);
            SignLocation signLocation = new SignLocation(1,100,100);

            String sealCode = "org001";
            String sealPassword = "11111111";
            String sealStrategyXml = StrategyUtil.createSealStrategyXml(sealCode, sealPassword, sealUserInfo, signLocation);

            long startTime = System.currentTimeMillis();
            for (int i = 0; i < 20; i++) {
                new SealPerformance(pdf, sealStrategyXml, startTime).start();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

}

class SealPerformance extends Thread {
    static int successCount;
    static int failueCount;
    static long startTime;

    private synchronized static void successAdd() {
        successCount++;
    }

    private synchronized static void failueAdd() {
        failueCount++;
    }

    String sealStrategyXml;
    byte[] pdf;

    public SealPerformance(byte[] pdf, String sealStrategyXml, long startTime) {
        super();
        this.pdf = pdf;
        this.sealStrategyXml = sealStrategyXml;
        this.startTime = startTime;
    }

    @Override
    public void run() {
        long timeStart = System.currentTimeMillis();
        boolean f = process(pdf, sealStrategyXml);
        System.out.println("time:" + ":" + (System.currentTimeMillis() - timeStart));
    }

    private boolean process(byte[] pdf, String sealStrategyXml) {
        try {
            for (int i = 0; i < 1000; i++) {
                PaperlessClient clientBean = new PaperlessClient(PaperlessConfig.url, 10000, 60000);
                String savedPdfId = GUID.generateId();
                pdf = FileUtils.readFileToByteArray(new File("./TestData/file.pdf"));
                byte[] sealedPdf = clientBean.sealAutoPdf(pdf, "", sealStrategyXml, PaperlessConfig.operatorCode, PaperlessConfig.channelCode);
                String errorRsString = PaperlessUtil.isError(sealedPdf);
                if ("".equals(errorRsString)) {
                    SealPerformance.successAdd();
                    IoUtil.write("./TestData/performance/" + savedPdfId + ".pdf", sealedPdf);
                } else {
                    System.out.println("error:" + errorRsString);
                    SealPerformance.failueAdd();
                }
                if(SealPerformance.successCount % 100 == 0) {
                    long period = (System.currentTimeMillis() - startTime)/1000;
                    long tps = SealPerformance.successCount / period;
                    System.out.println("tps:" + tps + ",period:" + period);
                }
//                Thread.sleep(100);
            }
            return true;
        } catch (Exception e) {
            return false;
        }
    }

}
